package com.wuzihao.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuzihao.reggie.common.R;
import com.wuzihao.reggie.entity.Category;
import com.wuzihao.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @PostMapping
    public R save(@RequestBody Category category){
        categoryService.save(category);

        return R.success("新增菜品成功");
    }

    @GetMapping("/page")
    public R pageInfo(int page,int pageSize){
        Page pageInfo = categoryService.pageInfo(page,pageSize);

        return R.success(pageInfo);
    }

    @DeleteMapping
    public R delete(Long id){
        categoryService.delete(id);

        return R.success("删除成功");
    }

    @PutMapping
    public R update(@RequestBody Category category){
        categoryService.updateById(category);

        return R.success("分类修改成功");
    }

    /**&
     * 菜品管理添加菜品时获取菜品分类
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        List<Category> list = categoryService.listCategory(category);

        return R.success(list);
    }
}
